﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using parkMonitor.entity;


//using System.Runtime.Serialization.Formatters.Binary;

namespace parkMonitor.Tools
{
    public class JsonByByteToObjectTools
    {
        public static byte[] ObjToJsonByte(AbstractMessage message)
        {
            string objstr = JsonConvert.SerializeObject(message);
            byte[] buffer = Encoding.UTF8.GetBytes(objstr);
            return buffer;
        }

        public static AbstractMessage JsonByteToObj<AbstractMessage>(byte[] bstr) where AbstractMessage : class
        {
            int count = 0;
            for (int i = 0; i < bstr.Length; i++)
            {
                if (bstr[i] == 0)
                {
                    count = i;
                    break;
                }
            }
            string str = new UTF8Encoding().GetString(bstr);

            AbstractMessage msg = JsonConvert.DeserializeObject<AbstractMessage>(str);

            return msg;
        }
    }
}